import gzip, parser2
#from sqlalchemy.orm import sessionmaker
#from sqlalchemy import create_engine
from schema import Session, User, trigger_text

f = open('fixtures/sessions', 'rb')
sp = parser2.parse_sessions(f)

s = sp.next()

db_writer = Writer()
while s = sp.next():
  db_writer.add_user(s['user_id'])
  db_writer.add_session(s)
  db_writer.add_queries(s['queries'])
  #db_writer.commit()

print s

engine = create_engine('postgres://alice:123@localhost:5432/pws', echo=True)
conn = engine.connect()

conn.execute(trigger_text)

#user_inserter = User.__table__.insert().prefix_with("OR REPLACE")
#user_inserter = user_inserter.values(id = s['user_id'])

#conn.execute(user_inserter)
DbSession = sessionmaker(bind=engine)
session = DbSession()

session.add(User(id= s['user_id']))

#session.add(Session(**s))
session.commit()